package com.cloud.wms.ui.in.inventory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.widget.ScanText;
import com.cloud.widget.expand.ExpandLayout;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.in.contract.InInventoryContract;
import com.cloud.wms.ui.in.entity.InOrderEntity;
import com.cloud.wms.ui.in.presnter.InInventoryPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.main.entity.TitleEntity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-20 15:22
 * @Description: 入库清点入库单列表页面
 */
public class InOrderActivity extends BaseBarListActivity<TitleEntity<InOrderEntity>, InInventoryPresenter> implements InInventoryContract.View {
    @BindView(R.id.iv_search)
    ImageButton ivSearch;
    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.btn_no_order)
    TextView btnNoOrder;
    @BindView(R.id.ll_btn)
    LinearLayout llBtn;
    private boolean isTaskFlow;
    private InOrderEntity orderEntity;
    private String taskCode;

    @Override
    protected String barTitle() {
        return "入库清点";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_in_order;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_common_member;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        isTaskFlow = getIntent().getBooleanExtra(IntentKey.TYPE_KEY, true);
        taskCode = getIntent().getStringExtra(IntentKey.INFO_KEY);
        llBtn.setVisibility(isTaskFlow ? View.GONE : View.VISIBLE);
        mPresenter.loadInOrderList("", page = 0);
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.loadInOrderList("", page = 0);
    }

    @Override
    protected boolean isRefresh() {
        return true;
    }

    @Override
    protected void loadListData(int page) {
        super.loadListData(page);
        mPresenter.loadInOrderList("", page = 0);
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        mPresenter.loadInOrderList(result, page = 0);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, TitleEntity<InOrderEntity> item) {
        helper.setText(R.id.tv_member_name, item.getTitle());
        ImageView ivArrow = helper.getView(R.id.iv_arrow);
        ExpandLayout expand = helper.getView(R.id.expand_layout);
        expand.setOnExpansionUpdateListener((expansionFraction, state) -> {
            ivArrow.setRotation(expansionFraction * 180);
        });
        if (mAdapter.getData().size() == 1) {
            helper.setGone(R.id.ll_title_content, true);
            expand.expand();
        }
        helper.getView(R.id.ll_title_content).setOnClickListener(v -> expand.toggle(true));

        RecyclerView rvChildList = helper.getView(R.id.rv_child_list);
        rvChildList.setLayoutManager(new LinearLayoutManager(InOrderActivity.this));
        BaseQuickAdapter mChildAdapter = new BaseQuickAdapter<InOrderEntity, BaseViewHolder>(R.layout.item_in_order, item.getChildList()) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, InOrderEntity item) {
                helper.setText(R.id.tv_in_code, item.getInCode())
                        .setText(R.id.tv_in_type, item.getInTypeName())
                        .setText(R.id.tv_in_sku, item.getSkuAmount())
                        .setText(R.id.tv_in_amount, item.getTotalAmount())
                        .setText(R.id.tv_in_create, item.getCreateDate());
            }
        };

        mChildAdapter.setOnItemClickListener((adapter, view, position) -> {
            orderEntity = (InOrderEntity) mChildAdapter.getData().get(position);
            if (isTaskFlow) {
                mPresenter.receiveInventoryTask(orderEntity, taskCode);
            } else {
                InTypeActivity.start(this, orderEntity);
            }
        });
        rvChildList.setAdapter(mChildAdapter);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case InInventoryPresenter.IN_TASK:
                InTypeActivity.start(this, ((TaskEntity) message.obj), orderEntity);
                break;
        }
    }

    @OnClick({R.id.iv_search, R.id.btn_no_order})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
                String text = scanText.getText();
                mPresenter.loadInOrderList(text, page = 0);
                break;
            case R.id.btn_no_order:
                if (isTaskFlow) {
                    info("当前功能需要任务才可执行！");
                } else {
                    InTypeActivity.start(this, null);
                }
                break;
        }
    }

    public static void start(Context context, boolean isTaskFlow) {
        Intent intent = new Intent(context, InOrderActivity.class);
        intent.putExtra(IntentKey.TYPE_KEY, isTaskFlow);
        context.startActivity(intent);
    }
}
